package hbsi.edu.demo2;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

/**
 * @author 程奕红
 * @version 1.0
 */
public class JdbcTest1 {
    public static void main(String[] args) {
        UpdateUser("Jerry","111111");

        //更新和删除
    }
    //数据库更新的方法
    public  static void UpdateUser(String name,String pass){
        Connection connection=null;
        Statement statement=null;

        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url="jdbc:mysql:///myjdbc";
            String username="root";
            String password="root";

            try {
                connection= DriverManager.getConnection(url,username,password);
                statement=connection.createStatement();
                System.out.println(connection);
                String sql=" update users set password='"+pass+"' where name='"+name+"';";
                int n= statement.executeUpdate(sql);
                if(n>0){
                    System.out.println("更新成功");
                }else{
                    System.out.println("更新失败");
                }
            } catch (SQLException e) {
                System.out.println(e);
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }finally {
            if(statement!=null){
                try {
                    statement.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
         if(connection!=null){
             try {
                 connection.close();
             } catch (SQLException e) {
                 throw new RuntimeException(e);
             }
         }

        }
    }
//    public class TestUsers{
//        public static void main(String [] args){
//
//        }
//    }
    public static  void login(String name,String pass){
        //访问数据库，查看数据库的用户名和密码是否输入正确
    }
}
